#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Stu
{
	char name[20];
	int age;
};

int com_int(const void* p1, const void* p2)
{
	return (*(int*)p1) - (*(int*)p2);
}
int com_name(const void* p1, const void* p2)
{
	
	return strcmp(((struct Stu*)p1)->name, ((struct Stu*)p2)->name);
}
int com_age(const void* p1, const void* p2)
{
	return ((struct Stu*)p1)->age - ((struct Stu*)p2)->age;
}
//int main()
//{
//	int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
//	struct Stu stu[3] ={{"zhangsan",20},{"lisi",25},{"wangwu",18} };
//	int sz1 = sizeof(arr) / sizeof(arr[0]);
//	int sz2 = sizeof(stu) / sizeof(stu[0]);
//	qsort(arr, sz1, sizeof(arr[0]), com_int);
//	qsort(stu, sz2, sizeof(stu[0]), com_name);
//	qsort(stu, sz2, sizeof(stu[0]), com_age);
//	return 0;
//}
void Swap(char* p1, char* p2, size_t width)
{
	for (int i = 0; i < width; i++)
	{
		int tmp = *p1;
		*p1 = *p2;
		*p2 = tmp;
		p1++;
		p2++;
	}
}
void bubble_sort(void* p1, size_t lenth, size_t width, int (*com)(const void*, const void*))
{
	int i = 0;
	for (int i = 0; i < lenth; i++)
	{
		int j = 0;
		for (int j = 0; j < lenth - 1 - i; j++)
		{
			if (com(((char*)p1 + j * width), ((char*)p1 + (j + 1) * width))>0)
			{
				Swap(((char*)p1+j*width),((char*)p1+(j+1)*width),width);
			}
		}
	}

}



int main()
{
	int arr[10] = { 9,8,7,6,5,4,3,2,1,0 };
	struct Stu stu[3] ={{"zhangsan",20},{"lisi",25},{"wangwu",18} };
	int sz1 = sizeof(arr) / sizeof(arr[0]);
	int sz2 = sizeof(stu) / sizeof(stu[0]);
	//bubble_sort(arr, sz1, sizeof(arr[0]), com_int);
	bubble_sort(stu, sz2, sizeof(stu[0]), com_name);
	//bubble_sort(stu, sz2, sizeof(stu[0]), com_age);
	return 0;
}